package UDP;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketAddress;

/**
 * @Author: ylc
 * @Description:
 * @Date Created in 2022-04-28 17:13
 */
public class UDPClient {
    public static void main(String[] args) throws IOException {
        /*
            - java.net.InetSocketAddress类
                实现IP套接字地址（IP地址+端口号）它也可以是一对（主机名+端口号）
                InetSocketAddress(InetAddress addr, int port)根据IP地址和端口号创建套接字地址。
         */
        SocketAddress ADDRESS = new InetSocketAddress("localhost", 9999);
        //创建客户端DatagramSocket，开启随机端口，可以发送及接收UDP数据报
        DatagramSocket socket = new DatagramSocket();

        byte[] bytes = "UDP通信".getBytes();
        //DatagramPacket(byte[] buf, int offset, int length, SocketAddress address)
        DatagramPacket packet = new DatagramPacket(bytes,0,bytes.length,ADDRESS);
        socket.send(packet);
    }
}
